<script setup lang="ts">
import { uploadLocal,askanswer } from '@/services/consult'
import {ref} from 'vue'
const show=true
const message=ref("")


const afterRead=(file)=>{
  uploadLocal(file.file)
    .then((res) => {
      console.log(res)
    })
    .catch(() => {
     alert('上传失败')
    })

}
const ask=ref('')
const source=ref('')

const connectToStream=()=> {
      
      source.value = new EventSource("http://localhost:8000/ask/?ask="+ask.value);

      source.value.onmessage =(event)=> {
          console.log(event)
          message.value = message.value + event.data
      };

        // this.source.onerror = (error=> {
        //     console.error('EventSource failed:', error);
        //     this.source.close();
        //     this.source = null;
        // });
}

// const askques=()=>{
//   alert(ask.value)
//   askanswer(ask.value).then((res)=>{
//     message.value = res.mes
//   }).catch(() => {
//      alert('fail')
//     })
// }

</script>

<template>
  <div class="patient-page">
   
   
    <!-- 使用 popup 组件 -->
    <van-popup position="right" v-model:show="show">
      <cp-nav-bar title="聊天"
        
      ></cp-nav-bar>
      <van-form autocomplete="off" ref="form">

        <van-uploader :after-read="afterRead" />
        <van-field
          v-model="ask"
          label="问题"
          placeholder="请输入问题"
         
        />
        
       
      </van-form>
      <div>
      答案:{{message}}
      </div>
      <!-- 删除按钮 -->
      <van-action-bar >
        <van-action-bar-button text="提交" @click="connectToStream()" />
      </van-action-bar>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.patient-page {
  padding: 46px 0 80px;
  :deep() {
    .van-popup {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      box-sizing: border-box;
    }
  }
}
.patient-change {
  padding: 15px;
  > h3 {
    font-weight: normal;
    margin-bottom: 5px;
  }
  > p {
    color: var(--cp-text3);
  }
}
.patient-next {
  padding: 15px;
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 80px;
  box-sizing: border-box;
}

// 底部操作栏
.van-action-bar {
  padding: 0 10px;
  margin-bottom: 10px;
  .van-button {
    color: var(--cp-price);
    background-color: var(--cp-bg);
  }
}

.patient-list {
  padding: 15px;
}
.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;
  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;
      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }
      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }
  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }
  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    .icon {
      color: var(--cp-primary);
    }
  }
}
.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
.pb4 {
  padding-bottom: 4px;
}
</style>
